package com.iye.puretennis.activity.test;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;
import com.iye.puretennis.R;
import com.iye.puretennis.activity.ProfileActivity;

import java.util.ArrayList;
import java.util.List;

/**
 * Author: Chuanbao on 2014/11/7.
 */
public class FriendsActivity extends Activity{
    private ListView listView;
    private ListView invitationsView;
    private FriendsAdapter adapter;
    private InvitationAdapter invitationAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.friends);

        listView = (ListView) findViewById(R.id.friends_list);
        invitationsView = (ListView) findViewById(R.id.friends_invitation_list);

        //prepare friends data
        User friend1 = new User("F0001","李四");
        User friend2 = new User("F0002","王五");
        User friend3 = new User("F0003","傻六");
        User friend4 = new User("F0004","雷老虎");
        User friend5 = new User("F0005","周中山");
        User friend6 = new User("F0006","李小六");
        User friend7 = new User("F0007","谢冰");
        User friend8 = new User("F0008","张忠");
        User friend9 = new User("F0009","毛欣");
        User friend10 = new User("F0010","礼泉");
        User friend11 = new User("F0011","梦晓");
        User friend12 = new User("F0012","巴明");

        List<User> friends = new ArrayList<User>();
        friends.add(friend1);
        friends.add(friend2);
        friends.add(friend3);
        friends.add(friend4);
        friends.add(friend5);
        friends.add(friend6);
        friends.add(friend7);
        friends.add(friend8);
        friends.add(friend9);
        friends.add(friend10);
        friends.add(friend11);
        friends.add(friend12);

//        listView.setOnItemClickListener(new ItemClickListener());
        adapter = new FriendsAdapter(this,friends,R.layout.friendslist);
        listView.setAdapter(adapter);

        //prepare friends data
        User invitation1 = new User("0004","王胖子");
        User invitation2 = new User("0005","刘二狗");
        User invitation3 = new User("0006","李大傻");
        User invitation4 = new User("0007","赵马子");
        User invitation5 = new User("0008","钱麻子");
        User invitation6 = new User("0009","孙麻子");
        User invitation7 = new User("0010","周麻子");
        User invitation8 = new User("0011","吴麻子");
        User invitation9 = new User("0012","同麻子");
        User invitation10 = new User("0013","细麻子");
        User invitation11 = new User("0014","臭麻子");
        User invitation12 = new User("0015","三马子");

        List<User> invitations = new ArrayList<User>();
        invitations.add(invitation1);
        invitations.add(invitation2);
        invitations.add(invitation3);
        invitations.add(invitation4);
        invitations.add(invitation5);
        invitations.add(invitation6);
        invitations.add(invitation7);
        invitations.add(invitation8);
        invitations.add(invitation9);
        invitations.add(invitation10);
        invitations.add(invitation11);
        invitations.add(invitation12);

        InvitationAdapter invitationAdapter = new InvitationAdapter(this,invitations,R.layout.friendsinvitationlist);
        invitationsView.setAdapter(invitationAdapter);
    }

    /**
     * 针对条目的点击事件
     */
    private final class ItemClickListener implements AdapterView.OnItemClickListener {

        /**
         * view:当前所点击条目的view对象
         * position:当前所点击的条目它所绑定的数据在集合中的索引值
         */
        public void onItemClick(AdapterView<?> parent, View view, int position,
                                long id) {
            ListView lView = (ListView) parent;
            /*针对show3()的显示 */
//            User friend = (User) lView.getItemAtPosition(position);
//            Toast.makeText(getApplicationContext(), friend.getName(),
//                    Toast.LENGTH_SHORT).show();
        }

    }

    class FriendsAdapter extends BaseAdapter{
        private List<User> friends;//在绑定的数据
        private int resource;//数据绑定在哪个资源界面上
        private LayoutInflater inflater;//布局填充器,可以使用xml文件来生成它的view对象

        private ViewCache viewCache;

        FriendsAdapter(Context context, List<User> friends, int resource) {
            this.friends = friends;
            this.resource = resource;
            inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        }

        @Override
        public int getCount() {
            return friends.size();
        }

        @Override
        public Object getItem(int position) {
            return friends.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        public void removeItem(int position){
            friends.remove(position);
            this.notifyDataSetChanged();
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            TextView nameView = null;
            ImageButton detailBtn = null;
            if(convertView == null){//为第一页显示,创建view对象
                convertView = inflater.inflate(resource, null);//null没有根元素
                nameView = (TextView) convertView.findViewById(R.id.name);
                detailBtn = (ImageButton) convertView.findViewById(R.id.action_details);
                viewCache = new ViewCache();
                viewCache.nameView = nameView;
                viewCache.detailBtn = detailBtn;
                //存放在标志里当作缓存使用
                convertView.setTag(viewCache);
            }else{
                viewCache = (ViewCache) convertView.getTag();
                nameView = viewCache.nameView;
                detailBtn = viewCache.detailBtn;
            }
            User friend = friends.get(position);
            nameView.setText(friend.getName());
            detailBtn.setOnClickListener(new DetailButtonListener(position));
            return convertView;
        }

        class DetailButtonListener implements View.OnClickListener{
            private int position;
            private DetailButtonListener(int position) {
                this.position = position;
            }

            @Override
            public void onClick(View view) {
                int id = view.getId();
                User friend = friends.get(position);
                if(id == viewCache.detailBtn.getId()){
                    Intent intent = new Intent(getApplicationContext(),ProfileActivity.class);
                    intent.putExtra("userGuid",friend.getGuid());
                    intent.putExtra("userName",friend.getName());
                    startActivity(intent);
                }
            }
        }
    }

    class InvitationAdapter extends BaseAdapter{

        private List<User> invitations;//在绑定的数据
        private int resource;//数据绑定在哪个资源界面上
        private LayoutInflater inflater;//布局填充器,可以使用xml文件来生成它的view对象

        private ViewCache viewCache;

        InvitationAdapter(Context context, List<User> invitations, int resource) {
            this.invitations = invitations;
            this.resource = resource;
            inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        }

        @Override
        public int getCount() {
            return invitations.size();
        }

        @Override
        public Object getItem(int position) {
            return invitations.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }
        public void removeItem(int position){
            invitations.remove(position);
            this.notifyDataSetChanged();
        }

        public void acceptInvitation(User invitationSender,int position){
            adapter.friends.add(invitationSender);
            adapter.notifyDataSetChanged();
            invitations.remove(position);
            this.notifyDataSetChanged();
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            TextView nameView = null;
            ImageButton rejectBtn = null;
            ImageButton acceptBtn = null;
            ImageButton detailBtn = null;
            if(convertView == null){//为第一页显示,创建view对象
                convertView = inflater.inflate(resource, null);//null没有根元素
                nameView = (TextView) convertView.findViewById(R.id.name);
                rejectBtn = (ImageButton) convertView.findViewById(R.id.action_reject);
                acceptBtn = (ImageButton) convertView.findViewById(R.id.action_accept);
                detailBtn = (ImageButton) convertView.findViewById(R.id.action_details);
                viewCache = new ViewCache();
                viewCache.nameView = nameView;
                viewCache.rejectBtn = rejectBtn;
                viewCache.acceptBtn = acceptBtn;
                viewCache.detailBtn = detailBtn;
                //存放在标志里当作缓存使用
                convertView.setTag(viewCache);
            }else{
                viewCache = (ViewCache) convertView.getTag();
                nameView = viewCache.nameView;
                rejectBtn = viewCache.rejectBtn;
                acceptBtn = viewCache.acceptBtn;
                detailBtn = viewCache.detailBtn;
            }
            User invitation = invitations.get(position);
            nameView.setText(invitation.getName());
            rejectBtn.setOnClickListener(new ButtonListener(position));
            acceptBtn.setOnClickListener(new ButtonListener(position));
            detailBtn.setOnClickListener(new ButtonListener(position));
            return convertView;
        }
        class ButtonListener implements View.OnClickListener{
            private int position;
            private ButtonListener(int position) {
                this.position = position;
            }

            @Override
            public void onClick(View view) {
                int id = view.getId();
                User friend = invitations.get(position);
                if(id == viewCache.rejectBtn.getId()){
                    removeItem(position);
                    Toast.makeText(getApplicationContext(),"reject invitation from:"+friend.name+friend.getGuid(),Toast.LENGTH_SHORT).show();
                }else if(id == viewCache.acceptBtn.getId()){
                    acceptInvitation(friend, position);
                    Toast.makeText(getApplicationContext(),"accept invitation from:"+friend.name+friend.getGuid(),Toast.LENGTH_SHORT).show();
                }else if(id == viewCache.detailBtn.getId()){
                    //TODO--sendRequest to server to delete this friend
                    removeItem(position);
                    Toast.makeText(getApplicationContext(),"remove friend:"+friend.name+friend.getGuid(),Toast.LENGTH_SHORT).show();
                }
            }
        }
    }


    /**
     * 该类用于缓存View对象,第一页的时候进行创建,第二页数据的时候使用缓存
     * 这种写法的另一个优点是代码数量少占用内存小,另一种是set/get方法
     */
    private final class ViewCache{
        public TextView nameView;
        public ImageButton rejectBtn;
        public ImageButton acceptBtn;
        public ImageButton detailBtn;
    }

    class User{
        String guid;
        String name;

        User(String guid, String name) {
            this.guid = guid;
            this.name = name;
        }

        public String getGuid() {
            return guid;
        }

        public void setGuid(String guid) {
            this.guid = guid;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }
    }


}
